<!DOCTYPE html>
<html>
<head>
    <title>layui</title>
    <#include "/back/common.html">
</head>
<body>

<form class="layui-form" id="userForm">
    <input type="hidden" id="formId" name="pk" value="${user.pk}">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户账号</label>
            <div class="layui-input-inline">
                <input type="text" name="account"  id="account" value="${user.account}" readonly="readonly"
                       lay-verify="account" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">用户电话</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" id="phone"  readonly="readonly" value="${user.phone}" lay-verify="phone" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><font color="red">※</font>旧密码</label>
            <div class="layui-input-inline">
                <input type="password" name="oldPassword" id="oldPassword"
                       lay-verify="notNull" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label"><font color="red">※</font>新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" id="password"
                       lay-verify="password" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>
<script>
    /*function checkUserPassword(){
      var ss =  $("#oldPassword").val();
       if(isNotBlank(ss)){
            $.ajax({
                url:"/user/checkUserPassword",
                data:{"pk":$("#formId").val(),"password":ss}
                success:function(resp){
                    if(resp.success){
                        layer.msg("密码校验成功");
                    }else{
                        layer.msg(resp.message);
                    }
                }
            })
       }else{
           layer.error("旧密码信息不能为空");
       }
    }*/


    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#birthday'
        });


        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            },
            notNull: function (value) {
                if (value.length <= 0) {
                    return '信息必填';
                }
            }
            , password: [
                /^[\w_-]{6,16}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , email: [
                /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , phone: [
                /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/
                , '手机号码格式异常'
            ]
            , account: [
                /^[A-Za-z0-9]{4,16}$/
                , '请输入4位以上的英文或数字组合'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            layui.use(['jquery'], function () {
                var $ = jQuery = layui.$;
                var ss =  $("#oldPassword").val();
                var ssN =  $("#password").val();
                if(isNotBlank(ss)){
                    $.ajax({
                        url:"/user/checkUserPassword",
                        data:{"pk":$("#formId").val(),"password":ss},
                        type:'post',
                        success:function(resp){
                            if(resp.success){
                                var postUrl = "/user/reset";
                                $.ajax({
                                    url: postUrl,
                                    type: 'post',
                                    data: $("#userForm").serialize(),
                                    success: function (resp) {
                                        if(resp.success){
                                            Shadow.success("密码修改成功,请重新登录");
                                            parent.layer.closeAll();
                                            window.location.reload();//刷新登录
                                        }else{
                                            Shadow.info(resp.message);
                                        }
                                    },
                                    error: function (resp) {
                                        Shadow.error(resp.message);
                                    }
                                });
                            }else{
                                Shadow.error(resp.message);
                            }
                        }
                    })
                }else{
                    Shadow.error("旧密码信息不能为空");
                }

            });
            return false;
        });
    });
</script>

</body>
</html>